#include "stdafx.h"
#include "LGUIDropDown.h"
#include "LGUIContainer.h"

LGUIDropDown::LGUIDropDown(HWND _hwnd, const TCHAR **_choices, int _start, int _numChoices, int id, int _x, int _y, int _w, int _h, int _userdata)
	: LGUIButton("", id,_x,_y,_w,_h)
{
	hwnd = _hwnd;
	start = _start;
	choices = _choices;
	numChoices = _numChoices;
	setUserData(_userdata);
	value = 0;
	ID = id;
	menu = CreatePopupMenu();
	AppendMenu(menu, 0, 0x100, "---");
	AppendMenu(menu, MF_MENUBARBREAK, 0,0);

	for (int i=_start; i<_start+_numChoices; i++)
	{
		if (choices[i][0] == '!')
			continue;
		const TCHAR *temp = choices[i];
		if (temp == 0)
			DebugBreak();
		if (temp[0] == '-')
		{
			AppendMenu(menu, MF_MENUBARBREAK, 0,0);
			temp++;
		}
		AppendMenu(menu, 0, 0x10 + i, temp);
	}
}


LGUIDropDown::~LGUIDropDown()
{
	DestroyMenu(menu);
}

void LGUIDropDown::penUp(int px, int py)
{
	if (isInside(px,py))
	{
		if (down && parent!=0)
		{
			DWORD dw = GetMessagePos();
			POINTS pt = MAKEPOINTS(dw);
			int id = TrackPopupMenuEx(menu, TPM_RIGHTBUTTON|TPM_RETURNCMD, pt.x, pt.y, hwnd, NULL);
			if (id)
			{
				value = id-0x10;
				parent->deliverMessage(LGUIMessage(MSG_VALUECHANGED,ID,value,userData));
			}
		}
		redraw();
	}

	LGUIButton::penUp(px,py);
}

void LGUIDropDown::draw(int xoff, int yoff)
{
	text = choices[value];
	if (text[0] == '-')
		text++;
	LGUIButton::draw(xoff,yoff);
}
